package com.examlpe.rabbitmqextendemo.listener;
import com.examlpe.rabbitmqextendemo.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class AckListener {
        @RabbitListener(queues = Constants.ACK_QUEUE)
        public void handMessage(Message message, Channel channel) throws Exception{
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            try {
                System.out.printf("接收消息: %s,deliveryTag: %d \n",
                        new String(message.getBody(),"UTF-8"), deliveryTag);
                System.out.println("业务逻辑处理...");
                //int num = 3/0;
                System.out.println("业务逻辑处理完!!!");
                channel.basicAck(deliveryTag,false);
            }catch (Exception e) {
                channel.basicNack(deliveryTag,false,true);
            }
        }
    /*@RabbitListener(queues = Constants.ACK_QUEUE)
    public void handMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("接收消息: %s,deliveryTag: %d \n",
                new String(message.getBody(), "UTF-8"), message.getMessageProperties().getDeliveryTag());
        System.out.println("业务逻辑处理...");
        int num = 3 / 0;
        System.out.println("业务逻辑处理完!!!");
        channel.basicAck(deliveryTag, false);
    }*/
}
